﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;

namespace WPFExcelReport
{
    public partial class TIVCL : ViewBase
    {
       
        #region ==dwTIVCL===========
        public static readonly DependencyProperty dwTIVCLProperty = DependencyProperty.Register("dwTIVCL",
            typeof(DataRow), typeof(TIVCL),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwTIVCLChanged));
        public DataRow dwTIVCL
        {
            get
            {
                return (DataRow)GetValue(dwTIVCLProperty);
            }
            set
            {
                SetValue(dwTIVCLProperty, value);
            }
        }
        private static void OndwTIVCLChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TIVCL;
            DataRow row = (DataRow)e.NewValue;
            if (src != null)
            {
                src.dwTIVCL = row;
            } 
        }
        #endregion
        #region ==FROMDATE===========
        public static readonly DependencyProperty FROMDATEProperty = DependencyProperty.Register("FROMDATE",
            typeof(string), typeof(TIVCL),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFROMDATEChanged));
        public string FROMDATE
        {
            get
            {
                return (string)GetValue(FROMDATEProperty);
            }
            set
            {
                SetValue(FROMDATEProperty, value);
            }
        }
        private static void OnFROMDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TIVCL;
            if (src != null)
            {
                src.FROMDATE = (string)e.NewValue;
            }
        }
        #endregion
        #region ==ENDDATE===========
        public static readonly DependencyProperty ENDDATEProperty = DependencyProperty.Register("ENDDATE",
            typeof(string), typeof(TIVCL),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnENDDATEChanged));
        public string ENDDATE
        {
            get
            {
                return (string)GetValue(ENDDATEProperty);
            }
            set
            {
                SetValue(ENDDATEProperty, value);
            }
        }
        private static void OnENDDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TIVCL;
            if (src != null)
            {
                src.ENDDATE = (string)e.NewValue;
            }
        }
        #endregion
        ITXEXRPT ITXEXRPT;
        DataSet Return;
        public TIVCL()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_VKCOST();
            IsMasterFromMV = true;
            InitializeComponent();
           
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {            
            base.Window_Loaded(sender, e);
            txtFACTORY.Focus();
        }        
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            
            if (e.Key == Key.Enter)
            {
                this.Focus();
                QueryCommand(null, null);
                txtFACTORY.Focus();
            }
        }
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {

                DataSet Result;
                Return = new DataSet();
                DataRow dwData = null;
                string ART = txtART.Text.ToString().Trim();
                string INV = txtINV.Text.ToString().Trim();
                string FACTORY = txtFACTORY.Text.ToString().Trim();
                string CURR = txtCURR.Text.ToString().Trim();
                if (FROMDATE == "")
                {
                    FROMDATE = "20000000";
                }
                if (ENDDATE == "")
                {
                    ENDDATE = "99999999";
                }
                if (FACTORY == "")
                {
                    FACTORY = "XXXXX";
                }
                if (CURR == "")
                {
                    CURR = "XXXXX";
                }
                if (INV == "")
                {
                    INV = "XXXXX";
                }
                if (ART == "")
                {
                    ART = "XXXXX";
                }

                dwData = TBMain.NewRow();
                dwData["VKPART"] = ART;
                dwData["INVDAT"] = FROMDATE;
                dwData["DELDAT"] = ENDDATE;
                dwData["CURRENCY"] = CURR;
                dwData["FACTORY"] = FACTORY;
                dwData["INVOICE"] = INV;

                Result = ((VICC_VKCOST)MV).QueryICC_VKCOST(dwData, 4);               
                //MergeData(Result, false);
                vwMaster.Source = Result.Tables["ICC_VKCOST"];
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            base.QueryCommand(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwTIVCL = dwMaster;
        }

        private void PrintCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ITXEXRPT == null)
                {
                    ITXEXRPT = new ITXEXRPT();
                    ITXEXRPT.Root = "C:\\IntexReport";
                    ITXEXRPT.strCompany = app.company;
                    ITXEXRPT.PrintType = "OpenExcel";
                }
                ITXEXRPT.dsSource = Return;
                //ITXEXRPT.dsSource.Merge(SetEnv);

                ITXEXRPT.DocType = "JLTCOSTLIST";

                ITXEXRPT.strExcelFile = @"\JLT\JLTView.xls";
                string filename = ITXEXRPT.WriteExcelReport();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
                
        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                FROMDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ENDDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
    }
}
